package com.lyl.cloud.controller;

import com.lyl.cloud.common.base.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
public class OrderController {

    @Value("${server.port}")
    private Integer serverPort;

    @GetMapping("/test")
    public String test(){
        String str = "hello,world";
        System.out.println("str = " + str);
        return str;
    }

    @GetMapping("/test2")
    public Result test2(){
        String str = "hello,world, return is a Result instance : 端口号为 " + serverPort ;
        System.out.println("str = " + str);
        return Result.success(str);
    }

    @GetMapping("/test/{id:\\d+}")
    public Result test3(@PathVariable("id") String id){
        List<String> list = Arrays.asList("1", "2", "3", "4");
        if (list.contains(id)){
            return Result.success("端口：" + serverPort + " 中的test3方法中包含 " + id);
        }
        return Result.success("端口：" + serverPort + " 中的test3方法中 " + id + " 不存在");
    }

    @GetMapping("/hystrix/ok/{id:\\d+}")
    public Result ok(@PathVariable("id") String id){
        String str = serverPort + "  : ok方法调用成功，id = " + id;
        System.out.println(str);
        return Result.success(str);
    }

    @GetMapping("/hystrix/timeout/{id:\\d+}")
    public Result timeout(@PathVariable("id") String id){
        try {
            TimeUnit.SECONDS.sleep(5);
            System.out.println(serverPort + "  : timeout进入等待，id = " + id);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        String str = serverPort + "  : timeout方法调用成功，id = " + id;
        System.out.println(str);
        return Result.success(str);
    }

    @GetMapping("/zipkin")
    public Result zipkin(){
        return Result.success("哈哈哈，😂😂😄，zipkin链路追踪");
    }
}
